package com.gxy.learn.optimisticlock.task;

import lombok.Data;

import java.math.BigDecimal;
import java.util.concurrent.*;

/**
 * 多线程取钱案例
 */
public class DrawMoneyTask {

//    ExecutorService executors =  Executors.newFixedThreadPool(5);
//ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
//        .setNameFormat("demo-pool-%d").build();
//
//    //Common Thread Pool
//    ExecutorService pool = new ThreadPoolExecutor(5, 200,
//            0L, TimeUnit.MILLISECONDS,
//            new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
//
//    pool.execute(()-> System.out.println(Thread.currentThread().getName()));
//    pool.shutdown();//gracefully shutdown




}


@Data
class Bank {
    private BigDecimal money;

    public BigDecimal drawMoney(BigDecimal dmoney) {
        synchronized (dmoney) {
            if (money.longValue() > 0) {
                if(money.compareTo(dmoney) >= 0){
                    BigDecimal subtract = money.subtract(dmoney);
                    setMoney(subtract);
                    return subtract;
                }
                return null;
            }
        }
        return null;
    }
}

